package com.xiaobao.coalproperty.module.user.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.BlankEntity;
import com.xiaobao.coalproperty.module.home.model.entity.GoodsDetailEntity;
import com.xiaobao.coalproperty.module.user.model.entity.AssetDetailEntity;
import com.xiaobao.coalproperty.other.adapter.PropertyAdapter;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

public class SaleOrderActivity extends BaseActivity {


    @BindView(R.id.sale_order_recycler)
    XRecyclerView mXRecyclerView;
    @BindView(R.id.order_sale_total)
    TextView tvSaleToal;
    @BindView(R.id.order_rmb)
    TextView tvRmb;
    @BindView(R.id.order_total)
    TextView tvToal;

    private View headView;
    private LinearLayout llEntrust;
    private EditText edtEntrustPrice;
    private TextView tvName, tvTime, tvPrice, tvGnum, tvSaleType, tvSalePrice, tvSaleRatio, tvSaleService, tvSaleDown, tvSaleUp, tvSaleNum;
    private PropertyAdapter mPropertyAdapter;

    private List<String> saleTypeList;
    private OptionsPickerView mOptionsPickerView;

    private String id;
    private AssetDetailEntity mAssetDetailEntity;

    @Override
    protected int getContentView() {
        return R.layout.activity_sale_order;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("资产详情");
        id = getIntent().getStringExtra(getIntent().getAction());
        mPropertyAdapter = new PropertyAdapter(this);
        mPropertyAdapter.setAdapterData(new ArrayList<GoodsDetailEntity.DataBean.PeopleInfoBean>());

        headView = LayoutInflater.from(this).inflate(R.layout.activity_sale_order_head, null, false);
        llEntrust = headView.findViewById(R.id.order_entrust_layout);
        tvName = headView.findViewById(R.id.order_name);
        tvTime = headView.findViewById(R.id.order_time);
        tvPrice = headView.findViewById(R.id.order_price);
        tvGnum = headView.findViewById(R.id.order_gnum);
        tvSaleType = headView.findViewById(R.id.order_type);
        tvSalePrice = headView.findViewById(R.id.order_sale_price);
        tvSaleRatio = headView.findViewById(R.id.order_sale_ratio);
        tvSaleService = headView.findViewById(R.id.order_sale_service);
        tvSaleDown = headView.findViewById(R.id.order_sale_down);
        tvSaleUp = headView.findViewById(R.id.order_sale_up);
        tvSaleNum = headView.findViewById(R.id.order_sale_num);
        edtEntrustPrice = headView.findViewById(R.id.order_entrust_price);

        tvSaleType.setOnClickListener(mOnClickListener);
        tvSaleDown.setOnClickListener(mOnClickListener);
        tvSaleUp.setOnClickListener(mOnClickListener);

        mXRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mXRecyclerView.addHeaderView(headView);
        mXRecyclerView.setLoadingListener(mLoadingListener);
        mXRecyclerView.setAdapter(mPropertyAdapter);

        saleTypeList = new ArrayList<>();
        saleTypeList.add("直接卖出");
        saleTypeList.add("委托卖出");
        mOptionsPickerView = new OptionsPickerView.Builder(this, mOnOptionsSelectListener).build();
        mOptionsPickerView.setPicker(saleTypeList);

        updateOrder();
    }

    private XRecyclerView.LoadingListener mLoadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            mXRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMore() {
            mXRecyclerView.loadMoreComplete();
        }
    };

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.order_type:
                    mOptionsPickerView.show();
                    break;
                case R.id.order_sale_down:
                    if (tvSaleNum.getText().toString().equals("1")) {
                        return;
                    }
                    tvSaleNum.setText((Integer.parseInt(tvSaleNum.getText().toString()) - 1) + "");
                    setServiceMoney();
                    break;
                case R.id.order_sale_up:
                    if (mAssetDetailEntity != null) {
                        if (tvSaleNum.getText().toString().equals(mAssetDetailEntity.getData().getGnum())) {
                            return;
                        }
                        tvSaleNum.setText((Integer.parseInt(tvSaleNum.getText().toString()) + 1) + "");
                        setServiceMoney();
                    }
                    break;
            }
        }
    };

    public void onSaleOrderClick(View view) {
        String sell_type = tvSaleType.getText().toString().equals("直接卖出") ? "1" : "2";
        String sell_money = sell_type.equals("1") ? tvSalePrice.getText().toString() : edtEntrustPrice.getText().toString();
        String sell_ratio = sell_type.equals("1") ? tvSaleRatio.getText().toString() : "";
        String sell_num = tvSaleNum.getText().toString();
        OkHttpUtils.post().url(UrlConstant.URL_ASSET_SUBMIT)
                .params(HttpHelperUtils.getInstance().assetSubmitParams(id, sell_type, sell_money, sell_ratio, sell_num))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BlankEntity entity = new Gson().fromJson(response, BlankEntity.class);
                    showToast(entity.getMsg());
                    if (entity.getCode() == 0) {
                        finish();
                    }
                } catch (Exception e) {
                }
            }
        });
    }

    private OptionsPickerView.OnOptionsSelectListener mOnOptionsSelectListener = new OptionsPickerView.OnOptionsSelectListener() {
        @Override
        public void onOptionsSelect(int options1, int options2, int options3, View v) {
            tvSaleType.setText(saleTypeList.get(options1));
            if (options1 == 0) {
                llEntrust.setVisibility(View.GONE);
                tvSaleToal.setVisibility(View.VISIBLE);
                tvRmb.setVisibility(View.VISIBLE);
                setServiceMoney();
            } else {
                llEntrust.setVisibility(View.VISIBLE);
                tvSaleToal.setVisibility(View.GONE);
                tvRmb.setVisibility(View.GONE);
                tvToal.setText("以卖出日为准");
                tvSaleRatio.setText("以卖出日为准");
                tvSaleService.setText("以卖出日为准");
            }
        }
    };

    private void updateOrder() {
        OkHttpUtils.post().url(UrlConstant.URL_ASSET_DETAIL)
                .params(HttpHelperUtils.getInstance().assetDetailParams(id))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    AssetDetailEntity entity = new Gson().fromJson(response, AssetDetailEntity.class);
                    if (entity.getCode() == 0) {
                        updateOrderSuccess(entity);
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }

    private void updateOrderSuccess(AssetDetailEntity entity) {
        mAssetDetailEntity = entity;
        tvName.setText(entity.getData().getGoods_name());
        tvTime.setText(entity.getData().getShop_time());
        tvGnum.setText(entity.getData().getGnum());
        tvPrice.setText(entity.getData().getMoney());
        tvSalePrice.setText(entity.getData().getSell_money() + "");
        setServiceMoney();
    }

    private void setServiceMoney() {
        try {
            tvSaleRatio.setText(mAssetDetailEntity.getData().getSell_ratio() + "");
            int saleNum = Integer.parseInt(tvSaleNum.getText().toString());
            double current = Double.parseDouble(tvSalePrice.getText().toString());
            tvSaleService.setText((saleNum * current - saleNum * current * mAssetDetailEntity.getData().getSell_ratio()) + "");
            tvToal.setText((saleNum * current) + "");
        } catch (Exception e) {
        }
    }

}
